🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / androidApp / src / main / kotlin / org / meshtastic / app / map / MbTilesImport.kt
Displaying Raw • Download
androidApp/src/main/kotlin/org/meshtastic/app/map/MbTilesImport.kt renovate/fastlane-2.x-lockfile (056eed64) Text, 3.04 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.app.map
Tff7b72import T7ee787android.content.Context
Tff7b72import T7ee787android.net.Uri
Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.withContext
Tff7b72import T7ee787org.meshtastic.core.common.util.ioDispatcher
Tff7b72import T7ee787java.io.File
Tff7b72import T7ee787java.io.FileOutputStream
Tff7b72import T7ee787java.io.IOException
T8b949e/** Where imported MBTiles archives live, kept apart from the imported-overlay directory. */
Tff7b72private Tff7b72const Tff7b72val Te6edf3MBTILES_DIR Tff7b72= Ta5d6ff"Ta5d6ffmbtilesTa5d6ff"
Tff7b72private Tff7b72const Tff7b72val Te6edf3TAG Tff7b72= Ta5d6ff"Ta5d6ffMbTilesImportTa5d6ff"
T8b949e/**
* Copies a picked MBTiles archive into app storage and returns its absolute path.
*
* The copy is not optional. A document picker hands back a `content://` URI backed by another app's provider, and
* MapLibre's MBTiles source opens a path with SQLite directly — it says so itself, in the one error string it carries:
* "MBTilesFileSource only supports absolute path urls". So the file has to be somewhere this process can name.
*
* Returns null if the copy fails, so a source is never stored pointing at a file that was never written.
*/
Tff7b72internal Tff7b72suspend Tff7b72fun Td2a8ffimportMbTilesTb4b4b4(Te6edf3contextTb4b4b4: Te6edf3ContextTb4b4b4, Te6edf3uriTb4b4b4: Te6edf3UriTb4b4b4, Te6edf3fileNameTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Tffa657String? Tff7b72= Te6edf3withContextTb4b4b4(Te6edf3ioDispatcherTb4b4b4) Tb4b4b4{
T8b949e// Every call below blocks, and an MBTiles archive is routinely hundreds of megabytes, so running on the
T8b949e// caller's dispatcher would freeze the UI for the whole copy — `suspend` alone does not move work off it.
Tff7b72try Tb4b4b4{
Tff7b72val Te6edf3input Tff7b72= Te6edf3contextTb4b4b4.Te6edf3contentResolverTb4b4b4.Te6edf3openInputStreamTb4b4b4(Te6edf3uriTb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3input Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3withTagTb4b4b4(Te6edf3TAGTb4b4b4)Tb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Ta5d6ffCould not open the picked MBTiles fileTa5d6ff" Tb4b4b4}
Tff7b72null
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72val Te6edf3directory Tff7b72= Te6edf3FileTb4b4b4(Te6edf3contextTb4b4b4.Te6edf3filesDirTb4b4b4, Te6edf3MBTILES_DIRTb4b4b4)Tb4b4b4.Te6edf3apply Tb4b4b4{ Tff7b72if Tb4b4b4(Tff7b72!Te6edf3existsTb4b4b4(Tb4b4b4)Tb4b4b4) Te6edf3mkdirsTb4b4b4(Tb4b4b4) Tb4b4b4}
Tff7b72val Te6edf3target Tff7b72= Te6edf3FileTb4b4b4(Te6edf3directoryTb4b4b4, Te6edf3fileNameTb4b4b4)
Te6edf3inputTb4b4b4.Te6edf3use Tb4b4b4{ Te6edf3source Tff7b72-Tff7b72> Te6edf3FileOutputStreamTb4b4b4(Te6edf3targetTb4b4b4)Tb4b4b4.Te6edf3use Tb4b4b4{ Te6edf3sink Tff7b72-Tff7b72> Te6edf3sourceTb4b4b4.Te6edf3copyToTb4b4b4(Te6edf3sinkTb4b4b4) Tb4b4b4} Tb4b4b4}
Te6edf3targetTb4b4b4.Te6edf3absolutePath
Tb4b4b4}
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3eTb4b4b4: Te6edf3IOExceptionTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3withTagTb4b4b4(Te6edf3TAGTb4b4b4)Tb4b4b4.Te6edf3eTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffCould not copy the picked MBTiles file into app storageTa5d6ff" Tb4b4b4}
Tff7b72null
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3eTb4b4b4: Te6edf3SecurityExceptionTb4b4b4) Tb4b4b4{
T8b949e// Not an IOException: the picker's URI grant can be gone by the time the copy runs, and the throw would
T8b949e// otherwise escape a function whose contract is to return null on failure.
Te6edf3LoggerTb4b4b4.Te6edf3withTagTb4b4b4(Te6edf3TAGTb4b4b4)Tb4b4b4.Te6edf3eTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffLost permission to read the picked MBTiles fileTa5d6ff" Tb4b4b4}
Tff7b72null
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.2 - Generated in 0.06s